//
//  MyCenterViewController.m
//  NewChat
//
//  Created by 马欢 on 16/2/27.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "MyCenterViewController.h"
#import "MyCenterViewCell.h"
#import "SuggestViewController.h"
#import "SettingsTableViewController.h"
#import "MyButtonView.h"
#import "MyDataViewController.h"
#import "UMSocial.h"
#import "MHDataHelper.h"
#import "pch.pch"
#import "PlayerManager.h"
#import "SongModel.h"
#import "PlayerViewController.h"
#import "UIImageView+WebCache.h"
#import "LoginViewController.h"
#import "AppDelegate.h"
#import "PlayerManager.h"


#define Manager [PlayerManager defaultManager]

@interface MyCenterViewController ()<UITableViewDataSource,UITableViewDelegate,MyDataViewControllerDelegate,UMSocialUIDelegate>
@property (weak, nonatomic) IBOutlet UITableView *myCenterTVC;
@property (weak, nonatomic) IBOutlet UIView *MyCenterView;
@property (nonatomic,strong)UIImageView *backgroundView;

@end

@implementation MyCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [[MHDataHelper shareDataHelper] requestAllSongsWithAllLike:AllLike didFinished:^(NSMutableArray *array) {
//        [PlayerManager defaultManager].passModels = array;
//    }];
    
    //   背景图片
    self.backgroundView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    self.backgroundView.image = [UIImage imageNamed:@"100.jpg"];
    [self.view addSubview:self.backgroundView];
    //  创建需要的毛玻璃特效类型
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    //  毛玻璃view 视图
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    //添加到要有毛玻璃特效的控件中
    effectView.frame = self.backgroundView.bounds;
    //设置模糊透明度（范围0到1）
    effectView.alpha = 0.6f;
    [self.backgroundView addSubview:effectView];
    [self.view sendSubviewToBack:effectView];
    [self.view sendSubviewToBack:self.backgroundView];
    // Do any additional setup after loading the view from its nib.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationItem.title = @"个人中心";
//    从数据库 调 用户信息

    
    self.nickL.font = [UIFont systemFontOfSize:18];
//    将有点击事件的view附在整个大View上
    MyButtonView *myView = [[MyButtonView alloc]initWithFrame:self.MyCenterView.frame];
    myView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:myView];
    [self.view bringSubviewToFront:myView];
    [myView myTarget:self action:@selector(toMyData)];//添加点击事件
    //    将playerPic设置成圆角
    self.myCenterImageV.layer.masksToBounds = YES;
    self.myCenterImageV.layer.cornerRadius = self.myCenterImageV.frame.size.width/2.0;
    self.label1.layer.masksToBounds = YES;
    self.label1.layer.cornerRadius = 10;
    self.label1.textAlignment = NSTextAlignmentCenter;
    self.label1.textColor = [UIColor whiteColor];
    self.label2.layer.masksToBounds = YES;
    self.label2.layer.cornerRadius = 10;
    self.label2.textAlignment = NSTextAlignmentCenter;
    self.label2.textColor = [UIColor whiteColor];
    //注册cell
    UINib *nib = [UINib nibWithNibName:@"MyCenterViewCell" bundle:nil];
    [self.myCenterTVC registerNib:nib forCellReuseIdentifier:@"MyCenterListCell"];
//    设置tableView代理
    self.myCenterTVC.dataSource = self;
    self.myCenterTVC.delegate = self;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 6;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
//设置cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyCenterViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCenterListCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.label.font = [UIFont systemFontOfSize:18];
    cell.moreL.textColor = [UIColor grayColor];
    if (indexPath.row == 0) {
        cell.label.text = @"音乐电台";
        cell.moreL.text = @"放松一下";
        cell.imageV.image = [UIImage imageNamed:@"diantai"];
    }else if (indexPath.row == 1){
        cell.label.text = @"推荐分享";
        cell.moreL.text = @"告诉好友";
        cell.imageV.image = [UIImage imageNamed:@"fenxiang"];
    }else if (indexPath.row == 2){
        cell.label.text = @"个人设置";
        cell.moreL.text = @"";
        cell.imageV.image = [UIImage imageNamed:@"shezhi"];
    }else if (indexPath.row == 3){
        cell.label.text = @"关于我们";
        cell.moreL.text = @"";
        cell.imageV.image = [UIImage imageNamed:@"women"];
    }else if (indexPath.row == 4){
        cell.label.text =@"意见反馈";
        cell.moreL.text =@"";
        cell.imageV.image = [UIImage imageNamed:@"jianyi"];

    }else if (indexPath.row == 5){
        cell.label.text =@"退出登录";
        cell.moreL.text =@"";
        cell.imageV.image = [UIImage imageNamed:@"tuichu"];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor clearColor];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
//cell点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //判断网络状态
    RealReachability *reachability = [RealReachability sharedInstance];
    ReachabilityStatus status = [reachability currentReachabilityStatus];
    if (status == RealStatusNotReachable){//无网
        
    }else{//有网
    if (indexPath.row == 0) {
        [self toMusicPlayer];
    }
    else if (indexPath.row == 1){
        [UMSocialSnsService presentSnsIconSheetView:self
                                             appKey:@"56dd84bfe0f55a58e80014bc"
                                          shareText:@"请输入你要分享的文字！！！"
                                         shareImage:nil
                                    shareToSnsNames:@[UMShareToSina,UMShareToQQ,UMShareToWechatSession,UMShareToWechatTimeline]
                                           delegate:self];
        
    }else if (indexPath.row == 2){
        SettingsTableViewController *settingsTVC = [SettingsTableViewController new];
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:settingsTVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;
    }else if (indexPath.row == 3){

        
        
        
        
    }else if (indexPath.row == 4){
        SuggestViewController *suggestVC = [[SuggestViewController alloc]init];
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:suggestVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;
    }else if (indexPath.row == 5){
        [PlayerManager defaultManager].player = nil;
        //        删除
        //        AVObject *findModel = [AVObject objectWithClassName:@"FindModel"];
        //        [findModel deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        //            if (succeeded) {
        //                NSLog(@"删除成功");
        //            }else{
        //                NSLog(@"删除失败");
        //            }
        //        }];
        
        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Login" bundle:[NSBundle mainBundle]];
        LoginViewController *presentVC = [storyBoard instantiateInitialViewController];
        
        
        NSLog(@"准备注销账号");
        
        //Leancloud退出
        [AVUser logOut];
        
        //环信退出
        
        NSLog(@"%@",[[EaseMob sharedInstance].chatManager loginInfo]);
        
        
        
        [[EaseMob sharedInstance].chatManager asyncLogoffWithUnbindDeviceToken:YES completion:^(NSDictionary *info, EMError *error) {
            
            NSLog(@"%@",info);
            
            if (!error) {
                [[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:NO];
                
                NSLog(@"退出成功");
                //保存在线状态为退出
                AVUser *user = [AVUser currentUser];
                [user setObject:@"离线" forKey:@"status"];
                [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                    if (succeeded) {
                        NSLog(@"离线状态保存成功");
                    }
                }];
                
                
                
                
                AppDelegate *temp = [UIApplication sharedApplication].delegate;
                temp.window.rootViewController = presentVC;
                [temp.window makeKeyAndVisible];
                
            }
        } onQueue:nil];
    }
  }//有网络
}
//大view点击事件 跳转到我的资料
-(void)toMyData{
    MyDataViewController *myDataVC = [MyDataViewController sharedMyDataViewController];
        self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:myDataVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;
}
//进入播放器听歌
-(void)toMusicPlayer{
    //判断网络状态
    RealReachability *reachability = [RealReachability sharedInstance];
    ReachabilityStatus status = [reachability currentReachabilityStatus];
    if (status == RealStatusNotReachable){
        
    }else{
        if ([MHDataHelper shareDataHelper].allSongsArray.count == 0) {
            [[MHDataHelper shareDataHelper] getData];
        }
    PlayerViewController *playerVC = [PlayerViewController sharedPlayerViewController];
            self.hidesBottomBarWhenPushed = YES;
        [self.navigationController showViewController:playerVC sender:nil];
            self.hidesBottomBarWhenPushed = NO;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//view将要出现 tabbar显示
-(void)viewWillAppear:(BOOL)animated{
    [self.tabBarController.tabBar setHidden:NO];
//    判断网络状态
    RealReachability *reachability = [RealReachability sharedInstance];
    ReachabilityStatus status = [reachability currentReachabilityStatus];
    if (status == RealStatusNotReachable) {//没有网
        NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
        self.myCenterImageV.image = [UIImage imageNamed:[user1 objectForKey:@"userImageName"]] ;
        self.nickL.text = [user1 objectForKey:@"nickname"];
        self.label1.text = [NSString stringWithFormat:@"%@ %@",[user1 objectForKey:@"gender"],[user1 objectForKey:@"age"]];
        self.label2.text = [user1 objectForKey:@"constellation"];
        self.idL.text = [NSString stringWithFormat:@"ID:%@",[user1 objectForKey:@"username"]];
    }else{//有网
        self.myCenterImageV.image = [UIImage imageNamed:[AVUser currentUser][@"userImageName"]];//头像
        self.nickL.text = [AVUser currentUser][@"nickname"];
        self.label1.text = [NSString stringWithFormat:@"%@ %@",[AVUser currentUser][@"gender"],[AVUser currentUser][@"age"]];
        self.label2.text = [AVUser currentUser][@"constellation"];
        self.idL.text = [NSString stringWithFormat:@"ID:%@",[AVUser currentUser][@"username"]];
    }
}
@end
